/*
## @file
#
#  Copyright (c) 2018 Loongson Technology Corporation Limited (www.loongson.cn).
#  All intellectual property rights(Copyright, Patent and Trademark) reserved.

#  Any violations of copyright or other intellectual property rights of the Loongson Technology
#  Corporation Limited will be held accountable in accordance with the law,
#  if you (or any of your subsidiaries, corporate affiliates or agents) initiate
#  directly or indirectly any Intellectual Property Assertion or Intellectual Property Litigation:
#  (i) against Loongson Technology Corporation Limited or any of its subsidiaries or corporate affiliates,
#  (ii) against any party if such Intellectual Property Assertion or Intellectual Property Litigation arises
#  in whole or in part from any software, technology, product or service of Loongson Technology Corporation
#  Limited or any of its subsidiaries or corporate affiliates, or (iii) against any party relating to the Software.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
#  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).
#
#
##
*/

//#define VREF 0x0701
#define COMPVREF_IN 1//1:in 0:out
#define COMP_MANUAL 0//1:manual 0:auto
#define COMP_PD     0
#define COMP_PCODE  0x8 //smaller, resistance smaller
#define COMP_NCODE  0xb//smaller, resistance bigger
#define COMP_CTRL   ((COMP_NCODE<<8)|(COMP_PCODE<<3)|(COMP_MANUAL<<2)|(COMPVREF_IN<<1)|(COMP_PD))
#define COMP_CTRL_OLD   ((COMP_NCODE<<8)|(COMP_PCODE<<3)|(1<<2)|(0<<1)|(COMP_PD))
//close vref_inen of both mc

//1. open mc0 conf space
//2. set mc_en to 2'b01
//3. clear vref_ctrl
//4. comp
//5. close mc0 conf space

//6. open mc1 conf space
//7. set mc_en to 2'b01
//8. clear vref_ctrl
//9. comp
//10. close mc0 conf space
//11. default mc_en is mc0

#define VREF_SET(VREF) \
        GET_NODE_ID_a0; \
;\
        dli     t0, 0x900000003ff00180 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        ori     t1, 0x10; ;\
        xori    t1, 0x10 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000003ff00400 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        lui     t2, 0x4000 ;\
        or      t1, t2 ;\
        lui     t2, 0x8000 ;\
        or      t1, t2 ;\
        xor     t1, t2 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000000ff00810 ;\
        or      t0, a0 ;\
        dli     t3, (VREF<<48)|(VREF<<32)|(VREF<<16)|VREF ;\
        sd      t3, 0x0(t0) ;\
        sd      t3, 0x8(t0) ;\
        sd      t3, 0x10(t0) ;\
;\
        dli     t2, COMP_CTRL ;\
        dli     t0, 0x900000000ff00000 ;\
        lw      t1, 0x0(t0) ;\
        li      t3, 0x10 ;\
        bne     t1, t3, 1f ;\
        nop ;\
        dli     t2, COMP_CTRL_OLD ;\
1: ;\
        sd      t2, 0x20(t0) ;\
;\
        dli     t0, 0x900000003ff00180 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        ori     t1, 0x10 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000003ff00180 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        ori     t1, 0x200 ;\
        xori    t1, 0x200 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000003ff00400 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        lui     t2, 0x8000 ;\
        or      t1, t2 ;\
        lui     t2, 0x4000 ;\
        or      t1, t2 ;\
        xor     t1, t2 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000000ff00810 ;\
        or      t0, a0 ;\
        sd      t3, 0x0(t0) ;\
        sd      t3, 0x8(t0) ;\
        sd      t3, 0x10(t0) ;\
;\
        dli     t2, COMP_CTRL ;\
        dli     t0, 0x900000000ff00000 ;\
        lw      t1, 0x0(t0) ;\
        li      t3, 0x10 ;\
        bne     t1, t3, 1f ;\
        nop ;\
        dli     t2, COMP_CTRL_OLD ;\
1: ;\
        sd      t2, 0x20(t0) ;\
;\
        dli     t0, 0x900000003ff00180 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        ori     t1, 0x200 ;\
        sw      t1, 0x0(t0) ;\
;\
        dli     t0, 0x900000003ff00400 ;\
        or      t0, a0 ;\
        lw      t1, 0x0(t0) ;\
        lui     t2, 0x4000 ;\
        or      t1, t2 ;\
        lui     t2, 0x8000 ;\
        or      t1, t2 ;\
        xor     t1, t2 ;\
        sw      t1, 0x0(t0)
